package com.biwin.common.app.vo;

import com.biwin.common.api.enums.IBwEnum;
import com.biwin.common.app.enums.BwBasicResponseEnum;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.biwin.common.api.constant.BwSystemConstant.SEMICOLON;

/**
 * BiWin framework common response
 *
 * @author biwin
 * @since 0.0.1
 */
public class BwResponse {
    private static final String formatSpecifier
            = "%(\\d+\\$)?([-#+ 0,(\\<]*)?(\\d+)?(\\.\\d+)?([tT])?([a-zA-Z%])";
    private static final Pattern fsPattern = Pattern.compile(formatSpecifier);

    private String code;
    private String message;
    private Object data;

    public BwResponse() {
    }

    public BwResponse(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public BwResponse(String code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public BwResponse(IBwEnum bwEnum) {
        this.code = bwEnum.getCode();
        this.message = bwEnum.getValue();
    }


    public BwResponse(IBwEnum bwEnum, String... message) {
        this.code = bwEnum.getCode();
        this.message = String.format(bwEnum.getValue(), message);
    }

    private BwResponse(IBwEnum bwEnum, Object data) {
        this.code = bwEnum.getCode();
        this.message = bwEnum.getValue();
        this.data = data;
    }

    public BwResponse(IBwEnum bwEnum, Object data, String... message) {
        this.code = bwEnum.getCode();
        Matcher matcher = fsPattern.matcher(bwEnum.getValue());
        if (matcher.matches()) {
            this.message = String.format(bwEnum.getValue(), message);
        } else {
            if (message.length > 0) {
                StringBuilder sb = new StringBuilder();
                sb.append(bwEnum.getValue());
                sb.append(SEMICOLON);
                for (String s : message) {
                    sb.append(s);
                    sb.append(SEMICOLON);
                }
                this.message = sb.deleteCharAt(sb.length()-1).toString();
            } else {
                this.message = bwEnum.getValue();
            }
        }
        this.data = data;
    }

    /**
     * 默认的成功操作的响应
     */
    public static BwResponse SUCCESS() {
        return new BwResponse(BwBasicResponseEnum.REQUEST_SUCCESS);
    }

    public static BwResponse SUCCESS(String message) {
        return new BwResponse(BwBasicResponseEnum.REQUEST_SUCCESS.getCode(), message);
    }

    public static BwResponse SUCCESS(Object data) {
        return new BwResponse(BwBasicResponseEnum.REQUEST_SUCCESS, data);
    }

    /**
     * 默认的失败操作的响应
     */
    public static BwResponse FAILED() {
        return new BwResponse(BwBasicResponseEnum.REQUEST_FAILED);
    }

    public static BwResponse FAILED(String message) {
        return new BwResponse(BwBasicResponseEnum.REQUEST_FAILED.getCode(), message);
    }

    public static BwResponse FAILED(Object data) {
        return new BwResponse(BwBasicResponseEnum.REQUEST_FAILED, data);
    }


    public static BwResponse response(IBwEnum bwEnum) {
        return new BwResponse(bwEnum);
    }

    public static BwResponse response(IBwEnum bwEnum, String... message) {
        return new BwResponse(bwEnum, message);
    }

    public static BwResponse response(IBwEnum bwEnum, Object data) {
        return new BwResponse(bwEnum, data);
    }

    public static BwResponse response(IBwEnum bwEnum, Object data, String... message) {
        return new BwResponse(bwEnum, data, message);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
